﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAOTAgency;

namespace TravelAgency
{
    public partial class WebFormTours : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected string GetDestination(int destinationId)
        {
            return DAO.DestinationNameById(destinationId);            
        }

        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            IQueryable<Tour> tours = null;
            if (CheckBoxDestination.Checked)
            {
                tours = DAO.SearchInToursByDestinationId(int.Parse(DropDownDestination.SelectedValue));
            }
            else
            {
                tours = DAO.GetAllTours();
            }

            if (CheckBoxPrice.Checked)
            {
                if (MinPriceValidator.IsValid && MaxPriceValidator.IsValid)
                {
                    decimal minPrice = decimal.Parse(TextBoxMinPrice.Text);
                    decimal maxPrice = decimal.Parse(TextBoxMaxPrice.Text);
                    tours = DAO.SearchInToursByPrice(minPrice, maxPrice, tours);
                }                
            }

            if (CheckBoxDate.Checked)
            {
                DateTime startDate = DateTime.Now;
                DateTime endDate = DateTime.Now;
                if (DateTime.TryParse(TextBoxStartDate.Text, out startDate) &&
                    DateTime.TryParse(TextBoxEndDate.Text, out endDate))
                {
                    tours = DAO.SearchInToursByDate(startDate, endDate, tours);
                }                
            }

            if (CheckBoxReferenceNumer.Checked)
            {
                tours = DAO.SearchInToursByReference(TextBoxReferenceNumber.Text, tours);
            }

            if (tours.Count() > 0)
            {
                RepeaterTours.DataSource = tours;
                RepeaterTours.DataBind();
                LabelNoResult.Text = "";
            }
            else
            {
                LabelNoResult.Text = "&lt; no tours matching tour criteria &gt;";
                RepeaterTours.DataBind();
            }
        }

        protected void GridViewTours_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] =
                   "this.style.background='#EEEEEE';this.style.cursor='hand'";
                e.Row.Attributes["onmouseout"] =
                    "this.style.background='white'";
                e.Row.Attributes["onclick"] = "location.href='WebFormDetails.aspx?TourID=" + DataBinder.Eval(e.Row.DataItem, "TourID") + "'";
            }
        }

        protected void GridViewTours_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView gr = (GridView)sender;
            ButtonSearch.Text = gr.DataMember;
        }
    }
}